import store from '../store'
import { isString, isObject } from './BaseUtil'
import * as XLSX from 'xlsx'

export const showLoading = (isLoading = false, text = '') => {
    if (typeof isLoading === 'string') {
        text = isLoading
        isLoading = true
    }
    return store.commit('widget/showLoading', { isLoading, text })
}
export const hideLoading = () => store.commit('widget/showLoading', false)
export const showMessage = (config) => store.commit('widget/showMessage', config)
export const showError = (config) => {
    const temp = {}
    if (isString(config)) {
        temp.message = config
        temp.type = 'error'
    } else if (isObject(config)) {
        Object.assign(temp, config, { type: 'error' })
    }
    store.commit('widget/showMessage', temp)
}
export const confirm = (message, title, config) => {
    const temp = { message, title, ...config }
    store.commit('widget/confirm', temp)
}

// 导出工具函数
export const exportToExcel = (data, headers, fileName) => {
  // 如果没有数据，返回
  if (!data || data.length === 0) {
    console.warn('没有数据可导出')
    return
  }

  // 创建工作表数据
  const wsData = []

  // 添加表头
  wsData.push(headers.map(h => h.label))

  // 添加数据行
  data.forEach(row => {
    const rowData = headers.map(header => {
      const value = row[header.prop]
      // 处理特殊字段
      if (header.prop === 'status') {
        return formatStatus(value)
      }
      if (header.prop === 'gender') {
        return formatGender(value)
      }
      if (header.prop === 'education') {
        return formatEducation(value)
      }
      if (header.prop === 'certificate_type') {
        return formatCertificateType(value)
      }
      if (header.prop === 'certificate_level') {
        return formatCertificateLevel(value)
      }
      if (header.prop === 'has_other_loans') {
        return formatBoolean(value)
      }
      if (header.prop === 'relationship') {
        return formatRelationship(value)
      }
      return value || ''
    })
    wsData.push(rowData)
  })

  // 创建工作簿
  const ws = XLSX.utils.aoa_to_sheet(wsData)
  const wb = XLSX.utils.book_new()
  XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')

  // 导出文件
  XLSX.writeFile(wb, `${fileName}_${formatDate(new Date())}.xlsx`)
}

// 格式化状态
const formatStatus = (status) => {
  const statusMap = {
    0: '待审核',
    1: '审核通过',
    2: '审核拒绝'
  }
  return statusMap[status] || '未知'
}

// 格式化性别
const formatGender = (gender) => {
  const genderMap = {
    '男': '男',
    '女': '女',
    'male': '男',
    'female': '女'
  }
  return genderMap[gender] || gender || ''
}

// 格式化学历
const formatEducation = (education) => {
  const educationMap = {
    '高中': '高中',
    '中专': '中专',
    '大专': '大专',
    '本科': '本科',
    '硕士': '硕士',
    '博士': '博士'
  }
  return educationMap[education] || education || ''
}

// 格式化证书类型
const formatCertificateType = (type) => {
  const typeMap = {
    1: '职业资格证书',
    2: '职业技能等级证书'
  }
  return typeMap[type] || type || ''
}

// 格式化证书等级
const formatCertificateLevel = (level) => {
  const levelMap = {
    1: '初级',
    2: '中级',
    3: '高级'
  }
  return levelMap[level] || level || ''
}

// 格式化布尔值
const formatBoolean = (value) => {
  if (value === true || value === 'true' || value === 1) return '是'
  if (value === false || value === 'false' || value === 0) return '否'
  return value || ''
}

// 格式化关系
const formatRelationship = (relationship) => {
  const relationshipMap = {
    '父亲': '父亲',
    '母亲': '母亲',
    '配偶': '配偶',
    '子女': '子女',
    '其他': '其他'
  }
  return relationshipMap[relationship] || relationship || ''
}

// 格式化日期
const formatDate = (date) => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return `${year}${month}${day}_${hours}${minutes}${seconds}`
}
